package com.tech.koufu.ui.activity;

 
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;  

import com.igexin.sdk.PushManager;
import com.tech.koufu.MainActivity;
import com.tech.koufu.MyApplication;
import com.tech.koufu.R;  
import com.tech.koufu.action.PushDemoReceiver.BundleClientIdListener;
import com.tech.koufu.tools.BuglyConfig;
import com.tech.koufu.tools.SharedConfig; 
import com.tech.koufu.ui.activity.login.BaseThridLoginActivity; 
import com.tencent.bugly.crashreport.CrashReport;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle; 
import android.os.Message;  
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener; 

public class WelcomeActivity extends BaseThridLoginActivity{
	private static final String TAG = WelcomeActivity.class.getName();
	private boolean first;	//判断是否第一次打开软件
	private View view;
	private Context context;
	private Animation animation;
	private SharedPreferences shared;
//	private StringRequest mRequest;
	private static int TIME = 1500; 		
	MyApplication myApp;
	public static final int AUTOLOGINFININSH = 0x219;
//	Handler handler = new Handler(); 
//	private RequestQueue mQueue;
	private boolean isRunLogin = false;
	public static BundleClientIdListener bundleClientIdListener;
	public static boolean isGetClientId = false;
	private boolean isBack = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); 
		ShareSDK.initSDK(this);  

		
		view = View.inflate(this, R.layout.activity_welcome, null);
		setContentView(view);
		context = this;							//得到上下文
		shared = new SharedConfig(context).GetConfig(); 	// 得到配置文件 
		myApp = (MyApplication)getApplication(); 

		PushManager.getInstance().initialize(this.getApplicationContext()); // SDK初始化，第三方程序启动时，都要进行SDK初始化工作 
		CrashReport.initCrashReport(getApplicationContext(), BuglyConfig.BUGAPPID ,BuglyConfig.ISDEBUG);  //初始化SDK     

		myApp.ClientID = PushManager.getInstance().getClientid(this);
		if(myApp.ClientID != null){
			Log.w("ClientID", myApp.ClientID);
		}
		CrashReport.setUserId("kofoo");  //设置用户的唯一标识 
		autoLogin();

	} 

	@Override
	protected void onResume() {  
		super.onResume();
		into();
	}

	public void onPause() {
		super.onPause();
	}
	
	// 进入主程序的方法
	private void into() {

		// 如果网络可用则判断是否第一次进入，如果是第一次则进入欢迎界面
		first = shared.getBoolean("First", true);

		// 设置动画效果是alpha，在anim目录下的alpha.xml文件中定义动画效果
		animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
		// 给view设置动画效果
		view.startAnimation(animation);
		animation.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation arg0) {
			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
			}

			// 这里监听动画结束的动作，在动画结束的时候开启一个线程，这个线程中绑定一个Handler,并
			// 在这个Handler中调用goHome方法，而通过postDelayed方法使这个方法延迟500毫秒执行，达到
			// 达到持续显示第一屏500毫秒的效果
			@Override
			public void onAnimationEnd(Animation arg0) {
				if(isRunLogin){
					 mHandler.postDelayed(new Runnable() {
						
						@Override
						public void run() {
							startNext(); 
						}
					}, TIME);
				}
			}
		});
	}
	
	/**
	 * 自动登录
	 */
	private void autoLogin(){ 
		isAutoLoginFlag = true;
		final String[] res = SharedConfig.getLoginStatus(getApplicationContext());
//		Log.v(TAG, res+"~~");
		if(res!=null){
			if(res[4].equals("qq")){
				qq = ShareSDK.getPlatform(QQ.NAME); 
				authorize(qq);
			}else{ 
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						 while(!isGetClientId){  
							 try {
								Thread.sleep(50);
							} catch (InterruptedException e) { 
								e.printStackTrace();
							}
						 } 
						 if(!isBack){
							 toLogin(res[0],res[1]);
						 }
					}
				}).start();
			}
			
		}else{
			isRunLogin = true;
		}
	}
	
	@Override
	public boolean handleMessage(Message msg) { 
		switch (msg.what) {
		case AUTOLOGINFININSH:
			startNext();
			break;

		default:
			break;
		}
		return true;
	}

	private void startNext() {
		Intent intent;
		//如果第一次，则进入引导页WelcomeActivity
		if (first) {
			intent = new Intent(WelcomeActivity.this,
					FirstAnimActivity.class);
		} else {
			intent = new Intent(WelcomeActivity.this,
					MainActivity.class);
		}
		startActivity(intent);
		// 设置Activity的切换效果
		overridePendingTransition(R.anim.in_from_right,
				R.anim.out_to_left);
		WelcomeActivity.this.finish();
	}
	@Override
	public void onBackPressed() { 
		super.onBackPressed();
		isBack = true;
	}
}
